import json
import os.path

import upyun

yun = upyun.UpYun('xxxx', username='xxxx', password='xxxx')

# headers = {'x-gmkerl-rotate': '180'}
headers = {}
failed_files = []


def list_files():
    data = []
    with open('./path_mapper.txt') as fd:
        data = json.load(fd)

    for file_item in data:
        up_load(file_item)


def up_load(filepath):
    # print(filepath)
    local_file = filepath.rstrip(r'.krc').__add__(r'.lrc')
    # print local_file
    if not os.path.exists(local_file):
        print ('file not found')
        failed_files.append(filepath)
        return
    remote_path = local_file.replace(r'__', r'/')
    remote_path = remote_path.lstrip(r'.')
    print (local_file, '==================' , remote_path)
    try:
        with open(local_file, 'rb') as f:
            res = yun.put(remote_path, f, checksum=True, headers=headers)
    except Exception, e:
        print (e)

        failed_files.append(filepath)


list_files()
print ('======================\nfailed to upload files:', failed_files)
